package travianBot.botScripts;

import java.util.HashMap;

import org.apache.log4j.Logger;

import travianBot.TravianBot;


/**
 * Class to contain the information needed to run the 
 * resource commands for the Travian bot
 * @author ikeddie
 *
 */
public class CommonResourceScript extends TravianScript {

	String[] 	resourceArray;
	int[]		levelArray;
	
	static Logger log=Logger.getLogger(CommonResourceScript.class);
	
	public CommonResourceScript(int cursorPosition, int loopCounter, String villageName, String type, String[] resourceArray) {
		this.cursorPosition = cursorPosition;
		this.loopCounter = loopCounter;
		this.villageName = villageName;
		this.resourceArray = resourceArray;
		this.type = type;
	}
	
	public CommonResourceScript(int cursorPosition, String villageName, String type, String[] resourceArray, int[] levelArray ){
		this.cursorPosition = cursorPosition;
		this.villageName = villageName;
		this.resourceArray = resourceArray;
		this.levelArray = levelArray;
		this.type = type;
	}
	
	public CommonResourceScript(String csvLine){
		init(csvLine);
	}
	
	public void init(String csvLine){
		String[] tokens = csvLine.split(",");
		
		int cursorToken 		= Integer.parseInt(tokens[0]);
		String villageNameToken = tokens[1];
		String typeToken 		= tokens[2];
		
		this.cursorPosition = cursorToken;
		this.villageName = villageNameToken;
		this.type = typeToken;
		
		// collect the remaining entries
		int tailLength = tokens.length - 3;
		
		String[] localResourceArray 		= new String[tailLength];
		int[] 	localResourceLevelArray 	= new int[tailLength];
		
		// collect the tail which is variable length
		String[] resourcePieces;
		String   resource;
		int      level;
		for (int i = 3; i < tokens.length; i++) {
			resourcePieces = tokens[i].split(":");
			localResourceArray[i-3]=resourcePieces[0];
			
			if(resourcePieces.length > 1){
				level = Integer.parseInt(resourcePieces[1]);
			}else{
				level = -1;
			}
			localResourceLevelArray[i-3] = level;
		}
		log.debug("creating resource script from: "+csvLine);
		log.debug("creating resource using tokens: "+cursorToken+","+villageNameToken+","+typeToken+","+localResourceArray+","+localResourceLevelArray);
		
		this.resourceArray = localResourceArray;
		this.levelArray = localResourceLevelArray;
	}
	
	public void runScript(TravianBot travianBot, HashMap<String,String> sessionInfoMap){
		
	}
	
	public String toString(){
		StringBuilder outString = new StringBuilder();
		outString.append(cursorPosition+","+villageName+","+type);
		
		for (int i = 0; i < this.resourceArray.length; i++) {
			String resource = this.resourceArray[i];
			int    level    = this.levelArray[i];
			outString.append(","+resource+":"+level);
		}
		
		return outString.toString();
	}
	
	public String getCurrentResourceUpgrade(){
		return resourceArray[cursorPosition];
	}
	
	public int getCurrentResourceLevel(){
		return levelArray[cursorPosition];
	}
	
	public void incrementResourceUpgradecounter(){
		cursorPosition = (cursorPosition + 1)% resourceArray.length;
	}

	public String getType() {
		return type;
	}

	public void setResourceAndLevel(String[] resourceArray, int[] levelArray) {
		this.resourceArray = resourceArray;
		this.levelArray = levelArray;
	}

	/* (non-Javadoc)
	 * @see travianBot.ResourceScript#getVillageName()
	 */
	public String getVillageName() {
		return villageName;
	}

	/* (non-Javadoc)
	 * @see travianBot.ResourceScript#setVillageName(java.lang.String)
	 */
	public void setVillageName(String villageName) {
		this.villageName = villageName;
	}
}
